#include "pet.h"

Pet::Pet(QObject *parent)
    : QObject(parent)
    , m_name("")
    , m_level(1)
    , m_hunger(50)
    , m_clean(50)
    , m_pleasure(50)
{
}

void Pet::setName(const QString &name)
{
    m_name = name;
}

QString Pet::getName() const
{
    return m_name;
}

int Pet::getLevel() const
{
    return m_level;
}

int Pet::getHunger() const
{
    return m_hunger;
}

int Pet::getClean() const
{
    return m_clean;
}

int Pet::getPleasure() const
{
    return m_pleasure;
}


//判断宠物是否活着
bool Pet::isAlive() const
{
    return m_hunger > 0 && m_clean > 0 && m_pleasure > 0;
}

void Pet::inHunger(int value)
{
    m_hunger = qMin(100, m_hunger + value);
    emit statusChange();
    checkLevelUp();
}

void Pet::inClean(int value)
{
    m_clean = qMin(100, m_clean + value);
    emit statusChange();
    checkLevelUp();
}

void Pet::inPleasure(int value)
{
    m_pleasure = qMin(100, m_pleasure + value);
    emit statusChange();
    checkLevelUp();
}

//时间流逝，值降低
void Pet::deHunger()
{
    m_hunger = qMax(0, m_hunger - 5);
    emit statusChange();
}

void Pet::deClean()
{
    m_clean = qMax(0, m_clean - 4);
    emit statusChange();
}

void Pet::dePleasure()
{
    m_pleasure = qMax(0, m_pleasure - 3);
    emit statusChange();
}

void Pet::checkLevelUp() //各满80升级，均降到50
{
    int oldLevel = m_level;
    if (m_hunger >= 80 && m_clean >= 80 && m_pleasure >= 80) {
        m_level++;
        m_hunger = 50;
        m_clean = 50;
        m_pleasure = 50;
        emit levelUpSignal(m_level);
    }
    if (m_level >= 3) {
        emit gameOver();
    }
}

QString Pet::getStatusImage() //改变主页面宠物状态图片
{
    int hunger = getHunger();
    int clean = getClean();
    int pleasure = getPleasure();

    if (hunger >= 60 && clean >= 60 && pleasure >= 60) {
        return ":/images/happy.png";
    }
    else if (hunger < 30 || clean < 30 || pleasure < 30) {
        return ":/images/sad.png";
    }
    else {
        return ":/images/normal.png";
    }
}

void Pet::resetName(const QString &name)
{
    m_name = name;
    emit statusChange();
}

void Pet::setLevel(int level)
{
    m_level = level;
    emit statusChange();
}

void Pet::setHunger(int hunger)
{
    m_hunger = qBound(0, hunger, 100);
    emit statusChange();
}

void Pet::setClean(int clean)
{
    m_clean = qBound(0, clean, 100);
    emit statusChange();
}

void Pet::setPleasure(int pleasure)
{
    m_pleasure = qBound(0, pleasure, 100);
    emit statusChange();
}

void Pet::reset()
{
    resetName("");
    setLevel(1);
    setHunger(50);
    setClean(50);
    setPleasure(50);
}
